<?php

namespace app\common\model;

use think\Model;

/**
 * 商品管理
 */
class Goods extends Model
{
    // 追加属性
    protected $append = [
        'market_price',
        'price',
        'discount',
        'spec_list'
    ];
    // public function sku(){
    //     return $this->hasMany('GoodsSku','goods_id','id');
    // }
    public function getSpecListAttr($value,$data){
        $spec = SpuSpec::where('goods_id',$data['id'])->select();
        foreach ($spec as &$value) {
            $value['spec_value'] = Spec::where('id',$value['spec_id'])->value('name');
            $value['spec_value_list'] = SpecValue::where('spec_id',$value['spec_id'])->select();
        }
        return $spec;
    }

    public function postage(){
        return $this->hasOne('Postage','id','postage_id');
    }
    public function getMarketPriceAttr($value,$data){
        return GoodsSku::where('goods_id',$data['id'])->min('market_price');
    }
    public function getPriceAttr($value,$data){
        return GoodsSku::where('goods_id',$data['id'])->min('price');
    }
    public function getDiscountAttr($value,$data){
        return GoodsDiscount::where('apply_type',4)->whereIn('apply_ids',$data['id'])->select();
    }
}